
package org.buildforce.tomcat;

/**
 * Copyright 2001-2005 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

/**
 * @author  Martin Todorov
 */
public class TomcatConfiguration
{

    /**
     * @parameter expression="${tomcatHome}" required="true"
     */
    private String tomcatHome;

    /**
     * @parameter expression="${host}" default-value="localhost" required="false"
     */
    private String host = "localhost";

    /**
     * @parameter expression="${port}" default-value="8080" required="false"
     */
    private int port = 8080;

    /**
     * @parameter expression="${serverPort}" default-value="8005" required="false"
     */
    private int serverPort = 8005;

    /**
     * @parameter expression="${timeout}" default-value="15000" required="false"
     */
    private int timeout = 15000;

    /**
     * @parameter expression="${webappName}" required="false"
     */
    private String webappName;


    public TomcatConfiguration()
    {
    }

    /**
     * Returns the absolute path to ${TOMCAT_HOME}/webapps/${webapp}.
     *
     * @return
     */
    public String getApplicationPath() throws TomcatConfigurationException
    {
        return getWebappsPath() +"/" +getWebappName();
    }

    /**
     * Returns the absolute path to ${TOMCAT_HOME}/webapps.
     *
     * @return
     */
    public String getWebappsPath() throws TomcatConfigurationException
    {
        return getTomcatHome() +"/webapps";
    }

    /**
     * This method will return tomcatHome, if one is set.
     * If not, it will try to resolve <code>System.getProperty("tomcat.home")</code>
     *
     * @return
     */
    public String getTomcatHome() throws TomcatConfigurationException
    {
        if (tomcatHome != null)
            return tomcatHome;

        String systemTomcatHome = System.getProperty("tomcat.home");

        if (systemTomcatHome != null)
            return systemTomcatHome;
        else
            throw new TomcatConfigurationException("Tomcat home is neither set in plugin's configuration," +
                                                   " nor as a system property!");
    }

    public void setTomcatHome(String tomcatHome)
    {
        this.tomcatHome = tomcatHome;
    }

    public String getHost()
    {
        return host;
    }

    public void setHost(String host)
    {
        this.host = host;
    }

    public String getWebappName()
    {
        return webappName;
    }

    public void setWebappName(String webappName)
    {
        this.webappName = webappName;
    }

    public int getPort()
    {
        return port;
    }

    public void setPort(int port)
    {
        this.port = port;
    }

    public int getServerPort()
    {
        return serverPort;
    }

    public void setServerPort(int serverPort)
    {
        this.serverPort = serverPort;
    }

    public int getTimeout()
    {
        return timeout;
    }

    public void setTimeout(int timeout)
    {
        this.timeout = timeout;
    }

}
